﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using PPI.Client.Logic.XML;
using PPI.Entities.XML;
using VMind.Global;

namespace PPI.Controls
{
    public partial class OutboxDatabase : UserControl
    {
        private EXmlOutboxBase m_SelectedOutbox;
        private EXmlOutboxBase m_EntityBase;
        private string m_SelectedConnectionString;

        public EXmlOutboxBase SelectedOutbox
        {
            get { return m_SelectedOutbox; }
        }
        public EXmlOutboxBase EntityXml
        {
            set { m_EntityBase = value; }
        }
        public string SelectedConnectionString
        {
            get
            {
                return m_SelectedConnectionString;
            }
            set
            {
                m_SelectedConnectionString = value;
            }
        }


        public event EventHandler<EventArgs> ChangeSelection;
        private void OnChangeSelection(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (ChangeSelection != null)
            {
                ChangeSelection(o, e);
            }
        }

        public OutboxDatabase()
        {
            InitializeComponent();
            this.grdOutbox.AutoGenerateColumns = false;
            this.grdOutbox.ReadOnly = false;
        }

        public void LoadAllOutboxes()
        {
            LXmlOutbox _serviceLogic = new LXmlOutbox(m_EntityBase);
            List<EXmlOutboxBase> _listOutBoxes = _serviceLogic.GetAll();
            grdOutbox.DataSource = _listOutBoxes;
        }

        public void LoadOutboxesToUpload()
        {
            LXmlOutbox _serviceLogic = new LXmlOutbox(m_EntityBase);
            List<EXmlOutboxBase> _listOutBoxes = _serviceLogic.GetOutboxesToUpload();
            grdOutbox.DataSource = _listOutBoxes;
        }
        public void LoadAllOutboxesToUpload()
        {
            List<EXmlOutboxBase> _listOutBoxes = LXmlOutbox.LoadAllOutboxes();
            grdOutbox.DataSource = _listOutBoxes;
        }

        private void grdOutbox_SelectionChanged(object sender, EventArgs e)
        {
            m_SelectedOutbox = null;
            if (grdOutbox.SelectedRows.Count > 0)
            {
                m_SelectedOutbox = (EXmlOutboxBase)(grdOutbox.SelectedRows[0].DataBoundItem);    
                OnChangeSelection(this,new EventArgs());
            }
        }

        public bool HasDatabases()
        {
                return (grdOutbox.SelectedRows.Count > 0);           
        }

        public void SetSelectionMultiple()
        {
            grdOutbox.MultiSelect = true;
        }

        public List<EXmlOutboxBase> GetChekeckDatabases()
        {
            List<EXmlOutboxBase> _databases = new List<EXmlOutboxBase>();
            EXmlOutboxBase _CheckedDatabase;
            
            foreach (DataGridViewRow r in grdOutbox.SelectedRows)
            {
                _databases.Add((EXmlOutboxBase)r.DataBoundItem);
            }

            return _databases;
        }

        private void grdOutbox_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int clickedRowIndex;
            int clickedColumnIndex;
            bool isChecked;

            clickedRowIndex = e.RowIndex;
            clickedColumnIndex = e.ColumnIndex;
            
            //if(clickedRowIndex > -1 && clickedColumnIndex > -1)
            //{
            //}
        }

        

    }
}
